package com.yonyou.pmclouds.processphoto.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.annotation.Insert;
import com.yonyou.pmclouds.basecom.entity.AdditionalPropertiesVOWrapper;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.service.CommonPropertiesProcessor;
import com.yonyou.pmclouds.outter.user.UserInfoQuery;
import com.yonyou.pmclouds.processphoto.entity.ProcessPhotoVO;
import com.yonyou.pmclouds.processphoto.rmiitf.ProcessPhotoMaintain;
import com.yonyou.pmclouds.processphoto.rmiitf.ProcessPhotoQuery;
import com.yonyou.pmclouds.workflow.aspect.PMApprove;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@Controller
@RequestMapping(value = "/mobile/processphoto")
public class ProcessphotoController {
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private ProcessPhotoMaintain maintain;
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private ProcessPhotoQuery query;
    @Autowired
    private UserInfoQuery userQuery;
    @Autowired
    private CommonPropertiesProcessor propertiesProcessor;

    private static final String[] USER_ID_FIELDS = {ProcessPhotoVO.CREATOR};
    private static final String[] USER_NAME_FIELDS = {ProcessPhotoVO.CREATOR_NAME};

    @Insert
    @RequestMapping(value = "/save")
    @ResponseBody
    @PMApprove
    public ProcessPhotoVO save(@RequestBody ProcessPhotoVO vo) throws BusinessException {
        ProcessPhotoVO pvo = maintain.save(vo);
        userQuery.tranUserInfoField(new ProcessPhotoVO[]{pvo}, USER_ID_FIELDS, USER_NAME_FIELDS);
        return pvo;
    }
    
    @RequestMapping(value = "/queryByProject")
    @ResponseBody
    public AdditionalPropertiesVOWrapper[] queryByProject(@RequestParam("pk_project") String pk_project) throws BusinessException {
        ProcessPhotoVO[] vos = query.queryByProject(pk_project);
        if(vos == null || vos.length == 0) {
            return null;
        }
        userQuery.tranUserInfoField(vos, USER_ID_FIELDS, USER_NAME_FIELDS);
        return propertiesProcessor.wrapAdditionalProps(vos);
    }
    
    @RequestMapping(value = "/getDetail")
    @ResponseBody
    public ProcessPhotoVO queryByPk(@RequestParam("pkProcessPhoto") String pkProcessPhoto) throws BusinessException {
        ProcessPhotoVO processPhoto = query.queryOne(pkProcessPhoto);
        userQuery.tranUserInfoField(new ProcessPhotoVO[]{processPhoto}, USER_ID_FIELDS, USER_NAME_FIELDS);
        return processPhoto;
        
    }
}
